﻿///用于存储项目核心数据
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization;

using HAUtil;

using HA.MT3DMS.DomainLayer;
using HA.ModFlow.Infrastructure;



namespace HA.Data
{
    [Serializable]
    /// <summary>
    /// 这个类用于表示核心数据
    /// </summary>
    public  class Project
    {

        #region 构造函数
        public Project()
        {



            //this._displayOption = new DisplayOption();

            this._displayOption2 = new DisplayOptions2.DisplayOption2();



            //初始化，各个子项目
            this._scatter2dData = new Scatter2DCollection();

            this._scatter3dData = new Scatter3DCollection();

            this._tinData = new TinCollection();

            this._solidData = new SolidCollection();


            _grid3dData = new Grid3dCollecion();


            _mapData = new MapDataCollecion();


            this._assistData = new AssistData();



            this._interpolationOption2D=new InterOption2D ();






            this.ReserveEvents();

            //现在统一将基本参数初始化，放在projectservce 的新建项目里


         


            //Grid3d gdNew = new Grid3d(10, 8, 4, 100, 100, 40, 0, 0, 0, 0);

            //this._grid3dData.Grids.Add(gdNew);

            //DefaultParamForModFlow param = new DefaultParamForModFlow();



            //_mfData = new ModFlowData(param,gdNew.ModFlowGrid);


            //Scatter2D s = new Scatter2D();

            ////this._scatter2dData.Datas.Add(s);


            //Coverage c = new Coverage();

            //ConcetualModel cm = new ConcetualModel();

            //this._mapData.Add(c);
            //this._mapData.Add(cm);


            //StressPeriod sp = new StressPeriod();
            //_mfData.StressPeriods.AddStressPeriod(sp);

            
            //Well well1 = new Well(1, 2, 1, 10, 8, 4);
            //well1.RechargeRates.Add(1);
            //Well well2 = new Well(3, 4, 2, 10, 8, 4);
            //well2.RechargeRates.Add(2);
            //_mfData.SourceSinks.Wells.AddWell(well1);
            //_mfData.SourceSinks.Wells.AddWell(well2);
            //_mfData.SourceSinks.Wells.Enabled = true;



 
        }

        #endregion

        #region 事件模型

        [field:NonSerialized]
        public event EventHandler<GridModifiedEventArgs> GridModified;

      

        [field:NonSerialized]
        public event EventHandler<WaterGridModifiedEventArgs> WaterGridModified;


        [field: NonSerialized]
        public event EventHandler<SCColleconChangedEventArgs> SCCollectionChanged;

 


        [field: NonSerialized]
        public event EventHandler CurrentDSChanged;


        [field: NonSerialized]
        public event EventHandler CurrentFlowVectorDataChanged;


        //[field: NonSerialized]

        //public event EventHandler CurrentDSGridChanged;





        [field: NonSerialized]
        public event EventHandler<ModelChangedEventArgs> ModFlowChanged;

        [field: NonSerialized]
        public event EventHandler<ModelChangedEventArgs> MT3dMSChanged;

        #endregion

        #region 属性数据


        string _projectVersion = "1.0.0.0";
        /// <summary>
        /// 记录项目的版本号，存储时一般使用应用程序的版本号
        /// </summary>
        public string ProjectVersion
        {
            get { return _projectVersion; }
            set { this._projectVersion = value; }        
        }



        ProjectViewPort _currentViewPort=new ProjectViewPort ();

        /// <summary>
        /// 存储当前视图
        /// </summary>
        public ProjectViewPort CurrentViewPort
        {
            get { return _currentViewPort; }
            set { _currentViewPort = value; }
        }




        DSGrid3d _currentDSGrid;


        /// <summary>
        /// 只在焦点引起的dataset数据类型发生变化时，调用此属性，事件的内部数据更新，通过参数更新数据
        /// </summary>
        public DSGrid3d CurrentDSGrid
        {
            get { return _currentDSGrid; }
            set
            {



                //用一个伪的相等方法

                //if (this._currentDSGrid.LstValue != value.LstValue)
                //{

                if (this._currentDSGrid != value)
                {


                    _currentDSGrid = value;

                    if (_currentDSGrid != null)
                    {
                        //当

                        //跟新等值线的显示条目
                        
                        //this.DisplayOption.Grid3d.Contour.DsMin = this._currentDSGrid.Min;

                        //this.DisplayOption.Grid3d.Contour.DsMax = this._currentDSGrid.Max;

                        //this.DisplayOption.Grid3d.Contour.BuildContourlines();
                        
                        //if (CurrentDSGridChanged!=null)
                        //{
                        //    CurrentDSGridChanged(this,new EventArgs());

                        //}

                        if (this._displayOption2 != null && this._displayOption2.DoGrid3dData != null && this._displayOption2.DoGrid3dData.Grid3d.Contour != null)
                        {
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMin = this._currentDSGrid.Min;
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMax = this._currentDSGrid.Max;
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.BuildContourlines();

                        }

                    }
                    else
                    {
                        //(_currentDSGrid == null) 可以简单的理解为没有选择显示 Head  DropDown等降深数据

                        if (this._displayOption2 != null && this._displayOption2.DoGrid3dData != null && this._displayOption2.DoGrid3dData.Grid3d.Contour != null)
                        {
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMin = 0;
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMax = 0;
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.BuildContourlines();

                            //this.DisplayOption2.DoGrid3dData.Grid3d.Contour.ContourLines.Clear();

                        }

                    }



                }

            }
        }


        /// <summary>
        /// 
        /// </summary>
        CellCenterDataSet3d _currentDSData;
        /// <summary>
        /// 当前显示的数据集数据
        /// </summary>
        public CellCenterDataSet3d CurrentDSData
        {
            get { return _currentDSData; }
            set {


                //避免数据的重复更新

                if (_currentDSData!=value)
                {

                    _currentDSData = value;


                    #region 

                    if (_currentDSGrid == null)
                    {
                        //如果_currentDSGrid为null，则使用_currentDSData的值进行等值线计算，例如  顶部高程TopElevation

                        if (_currentDSData != null)
                        {
                            
                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMin = this._currentDSData.MinValue;

                            this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMax = this._currentDSData.MaxValue;

                            this._displayOption2.DoGrid3dData.Grid3d.Contour.BuildContourlines();
                        }
                    }
                   

                    #endregion

                    this.CurrentDSChanged(this, new EventArgs());

                    }
                    
                //}

               
            
            }
        }
        

        CellCenterVector3dDS  _curentFlowVectorData;
        /// <summary>
        /// CCF 向量数据
        /// </summary>
        public CellCenterVector3dDS CurentFlowVectorData
        {
            get { return _curentFlowVectorData; }
            set {

                if (this._curentFlowVectorData !=value )
                {

                      _curentFlowVectorData = value;


                      this.CurrentFlowVectorDataChanged(this, new EventArgs());


                }

            
            }
        }



        HA.ModFlow.Infrastructure.Units _units = new ModFlow.Infrastructure.Units() { Time = TIME.DAY, Length = LENGTH.M };

        public HA.ModFlow.Infrastructure.Units Units
        {
            get { return _units; }
        }



        InterOption2D _interpolationOption2D;

        public InterOption2D InterpolationOption2D
        {
            get { return _interpolationOption2D; }
           
        }


        //DisplayOption _displayOption;

        //public DisplayOption DisplayOption
        //{
        //    get { return _displayOption; }
        //}



        private DisplayOptions2.DisplayOption2 _displayOption2;
        /// <summary>
        /// 二次重构显示选项模块添加的字段属性  为不影响代码 上述字段_displayOption暂时不删除
        /// </summary>
        public DisplayOptions2.DisplayOption2 DisplayOption2
        {
            get { return _displayOption2; }
            //set { _displayOption2 = value; }
        }




        Grid3dCollecion _grid3dData;

        public Grid3dCollecion Grid3dData
        {
            get { return _grid3dData; }
            set { _grid3dData = value; }
        }




        Scatter2DCollection _scatter2dData;

        public Scatter2DCollection Scatter2dData
        {
            get { return _scatter2dData; }
            set { _scatter2dData = value; }
        }


        Scatter3DCollection _scatter3dData;

        public Scatter3DCollection Scatter3dData
        {
            get { return _scatter3dData; }
            set { _scatter3dData = value; }
        }


        TinCollection _tinData;

        public TinCollection TinData
        {
            get { return _tinData; }
            set { _tinData = value; }
        }


        SolidCollection _solidData;

        public SolidCollection SolidData
        {
            get { return _solidData; }
            set { _solidData = value; }
        }

        MapDataCollecion _mapData;

        public MapDataCollecion MapData
        {
            get { return _mapData; }
            
        }




        ModFlowData _mfData;

        /// <summary>
        /// 主模型数据
        /// </summary>
        public ModFlowData MfData
        {
            get { return _mfData; }
          
        }




        MT3DMSData _mt3dmsData;

        public MT3DMSData Mt3dmsData
        {
            get { return _mt3dmsData; }
          
        }


        AssistData _assistData;

        public AssistData AssistData
        {
            get { return _assistData; }
            
        }



        #endregion

        #region 辅助方法 
        

      


        #endregion

        #region 受保护的方法

        public void OnCurrentDSChanged()
        {
            if (_currentDSGrid == null)
            {
                //如果_currentDSGrid为null，则使用_currentDSData的值进行等值线计算，例如  顶部高程TopElevation

                if (_currentDSData != null)
                {

                    this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMin = this._currentDSData.MinValue;

                    this._displayOption2.DoGrid3dData.Grid3d.Contour.DsMax = this._currentDSData.MaxValue;

                    this._displayOption2.DoGrid3dData.Grid3d.Contour.BuildContourlines();
                }
            }

            if (this.CurrentDSChanged != null)
                this.CurrentDSChanged(this, new EventArgs());
        }

        protected void ReserveModflowEvents()
        {
            this.MfData.GridData.WaterGrid.WaterGridModified += new EventHandler<WaterGridModifiedEventArgs>(WaterGrid_WaterGridModified);



            //ModFlow相关的 事件
            this.MfData.HfbData.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Rivers.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Wells.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Drains.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.GeneralHeads.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.SpecifiedHeads.SCCollecionChanged += new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

        }


        protected void DeserveModflowEvents()
        {
            this.MfData.GridData.WaterGrid.WaterGridModified -= new EventHandler<WaterGridModifiedEventArgs>(WaterGrid_WaterGridModified);



            //ModFlow相关的 事件
            this.MfData.HfbData.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Rivers.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Wells.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.Drains.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.GeneralHeads.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

            this.MfData.SourceSinks.SpecifiedHeads.SCCollecionChanged -= new EventHandler<SCColleconChangedEventArgs>(SourceSink_CollecionChanged);

 
        }




        void SourceSink_CollecionChanged(object sender, SCColleconChangedEventArgs e)
        {

            if (this.SCCollectionChanged != null)
            {
                this.SCCollectionChanged(sender, e);
            }
        }

        #region 源汇采用同一事件处理方法 以下废弃 保改
        //void GeneralHeads_SCCollecionChanged(object sender, SCColleconChangedEventArgs e)
        //{
        //    if (this.SCCollectionChanged != null)
        //    {
        //        this.SCCollectionChanged(sender, e);

        //    }

        //}

        //void Drains_SCCollecionChanged(object sender, SCColleconChangedEventArgs e)
        //{
        //    if (this.SCCollectionChanged != null)
        //    {
        //        this.SCCollectionChanged(sender, e);

        //    }
        //}

        //void Wells_SCCollecionChanged(object sender, SCColleconChangedEventArgs e)
        //{
        //    if (this.SCCollectionChanged != null)
        //    {
        //        this.SCCollectionChanged(sender, e);

        //    }
        //}

        //void Rivers_SCCollecionChanged(object sender, SCColleconChangedEventArgs e)
        //{
        //    if (this.SCCollectionChanged!=null)
        //    {
        //        this.SCCollectionChanged(sender,e);
                
        //    }

        //}
        #endregion

        void WaterGrid_WaterGridModified(object sender, WaterGridModifiedEventArgs e)
        {


            if (WaterGridModified!=null)
            {
                WaterGridModified(sender,e);
                
            }
        }

       


        protected void ReserveEvents()
        {
            this.Grid3dData.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(Grid3dData_CollectionChanged);
            this.MapData.ConceputalModelCollectionChanged += new EventHandler<CollecionChangedEventArgs>(MapData_CollectionChanged);

            if (this.Grid3dData.MainGrid!=null)
            {
                this.Grid3dData.MainGrid.ModFlowGrid.GridModified += new EventHandler<GridModifiedEventArgs>(ModFlowGrid_GridModified);
                
            }

            if (this._mfData!=null)
            {

                this.ReserveModflowEvents();
            }
            if (this._displayOption2 !=null )
            {
                this._displayOption2.ReserveEvents();
            }

        }

        void MapData_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {
           


        }


       /// <summary>
       /// 三维网格集合的监听
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        void Grid3dData_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {
            Grid3d gd = (Grid3d)e.Child;

            switch (e.Type)
            {
                case CollectionChangedType.ADD:

                    gd.ModFlowGrid.GridModified += new EventHandler<GridModifiedEventArgs>(ModFlowGrid_GridModified);

                    break;
                case CollectionChangedType.REMOVE:
                    gd.ModFlowGrid.GridModified -= new EventHandler<GridModifiedEventArgs>(ModFlowGrid_GridModified);  


                  


                    break;
                case CollectionChangedType.MOVE:
                    break;
                case CollectionChangedType.REPLACE:
                    gd.ModFlowGrid.GridModified += new EventHandler<GridModifiedEventArgs>(ModFlowGrid_GridModified);

                    break;
                case CollectionChangedType.RESET:
                    break;
                default:
                    break;
            }




            //网格的删除或者重置，将导致所有数据集的变化，也就是重置,这里将删除一切等值线显示

            //重置对应的信息

            this.CurrentDSData = null;

            this.CurentFlowVectorData = null;

            this.CurrentViewPort.Number = 1;

            this.CurrentViewPort.ViewType = ProjectViewType.TOP;





        }



        void ModFlowGrid_GridModified(object sender, GridModifiedEventArgs e)
        {

           // this.MfData.GridData.WaterGrid.WaterGridModified += new EventHandler<WaterGridModifiedEventArgs>(WaterGrid_WaterGridModified);


            if (GridModified!=null)
            {
                GridModified(sender,e);
                
            }
        }



        #endregion

        #region 对外公共功能函数


        public void NewModflow()
        {

            //如果存在的话，就先删除

            if (this._mfData!=null)
            {
                this.DeleteModflow();
                
            }




            Grid3d gd = this.Grid3dData.MainGrid;
            if (gd!=null)
            {
                DefaultParamForModFlow param = new DefaultParamForModFlow();

                this._mfData = new ModFlowData(param, gd.ModFlowGrid, _units);


                this.ReserveModflowEvents();
              
            }



            if (ModFlowChanged != null)
            {
              

                ModFlowChanged(this,new ModelChangedEventArgs( ModelChangedType.CRREATED));

            }
 
        }



        public void DeleteMT3dMs()
        {
            if (this._mt3dmsData!=null)
            {
                this._mt3dmsData = null;
                
            }


            if (MT3dMSChanged != null)
            {
                MT3dMSChanged(this, new ModelChangedEventArgs(ModelChangedType.DELETED));

            }



        }


        public void DeleteModflow()
        {

            if (this._mt3dmsData!=null)
            {
                this.DeleteMT3dMs();
                
            }


            this.DeserveModflowEvents();

            this._mfData = null;


            //对于删除，需要给出对应的事件啊
            if (ModFlowChanged!=null)
            {
                //ModFlowChanged(this,new EventHandler());

                ModFlowChanged(this, new ModelChangedEventArgs(ModelChangedType.DELETED));

                
            }




 
        }



        /// <summary>
        /// 创建mt3d程序
        /// </summary>
        public void NewMt3dms()
        {
            if (this._mfData==null)
            {
                return;
                


            }

            this._mt3dmsData = new MT3DMSData(this._mfData);


            if (MT3dMSChanged!=null)
            {
                MT3dMSChanged(this,new ModelChangedEventArgs(ModelChangedType.CRREATED));
                
            }
 
        }



     


        #endregion

        #region 反序体制

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {


            this.ReserveEvents();

            if (_projectVersion == null)
            {

                //默认给它第一个应用程序版本的版本号吧  哈哈
                _projectVersion = "1.0.0.0";

            }
            else 
            {
                if (_projectVersion.Trim ()=="")
                {
                     _projectVersion = "1.0.0.0";
                }
            
            }



        }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {




        }

        #endregion


    }
}
